﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Partial Class FrontEnd_Control_Menu_LolitaLeftMenu
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim CatId As Integer = 809
            If Not Page.RouteData.Values("CateId") Is Nothing Then
                CatId = getRootMenu(Page.RouteData.Values("CateId"))
            End If


            BindMenu(CatId)
        End If
    End Sub
    Protected Sub BindMenu(ByVal CatId As Integer)
        Using Data As New VSEntities
            Dim p = (From q In Data.Categories Where q.CatId = CatId Select q Order By q.Sort).ToList
            With rptMenu
                .DataSource = p
                .DataBind()
            End With
        End Using
    End Sub
    'lay nhanh goc cua category theo catid truyen vao
    Protected Function getRootMenu(ByVal _catId As Integer) As Integer
        Dim _hientaiMenuId = _catId
        Using Data As New VSEntities
            Dim p = (From q In Data.Categories Where q.CatId = _hientaiMenuId Select q).FirstOrDefault
            If p Is Nothing Or p.ParentId = 0 Then
                Return _hientaiMenuId
            Else
                Return getRootMenu(p.ParentId)
            End If
        End Using
        Return _hientaiMenuId
    End Function

    Protected Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)
        'Dim hplTitle As HyperLink = e.Item.FindControl("hplTitle")
        Dim lblTitle As Label = e.Item.FindControl("lblTitle")
        lblTitle.Text = row.Title

        'hplTitle.NavigateUrl = MainDomain & "san-pham/" & row.QueryString & "-" & row.CatId & ".html"

        'If Page.RouteData.Values("Id") = row.CatId Then
        '    hplTitle.CssClass = "active"
        'End If

        Dim rptMenuChild As Repeater = CType(e.Item.FindControl("rptMenuChild"), Repeater)



        Using Data As New VSEntities
            Dim p = (From q In Data.Categories Where q.ParentId = row.CatId).ToList
            If p.Count > 0 Then
                rptMenuChild.DataSource = p
                rptMenuChild.DataBind()
            End If
        End Using


    End Sub
    Protected Sub rptMenuChild_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & "san-pham/" & row.QueryString & "-" & row.CatId & ".html"
        Dim rptMenuThird As Repeater = CType(e.Item.FindControl("rptMenuThird"), Repeater)

        Using Data As New VSEntities
            'Lay toan bo cap con cua Cate

            Dim p = (From q In Data.Categories Where q.ParentId = row.CatId).ToList
            rptMenuThird.DataSource = p
            rptMenuThird.DataBind()

        End Using
    End Sub
    Protected Sub rptMenuThird_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & "san-pham/" & row.QueryString & "-" & row.CatId & ".html"
        Dim rptMenuThird As Repeater = CType(e.Item.FindControl("rptMenuFourth"), Repeater)

        Using Data As New VSEntities
            'Lay toan bo cap con cua Cate

            Dim p = (From q In Data.Categories Where q.ParentId = row.CatId).ToList
            rptMenuThird.DataSource = p
            rptMenuThird.DataBind()

        End Using
    End Sub
    Protected Sub rptMenuFourth_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & "san-pham/" & row.QueryString & "-" & row.CatId & ".html"

    End Sub
End Class
